<?php
namespace Zaya\Lib\Exceptions;

use Throwable;

class SimpleErrorException extends \Exception {
    public function __construct($message, int $code = 400, Throwable $previous = null)
    {
        parent::__construct($message, $code, $previous);
    }

    public function render() {
        if (request()->ajax()) {
            return response()->json([
                'code' => $this->getCode(),
                'message' => $this->getMessage(),
            ])->send();
        } elseif (request('callback')) {
            // todo 优化 jsonp 支持，增加权限验证
            return response()->jsonp(request('callback'), [
                'code' => $this->getCode(),
                'message' => $this->getMessage(),
            ])->send();
        } else {
            return response(view('errors.simple', ['message' => $this->getMessage()])->render(), max($this->getCode(), 400));
        }
    }
}